/*
 * Copyright 2009 Jinbo Lin. All rights reserved.
 * 
 * cn.edu.pku.dao.impl HumanResourcesManagementSystem
 *
 * Created on 2009-12-20
 */
package cn.edu.pku.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.edu.pku.dao.PositionmgtDao;
import cn.edu.pku.model.Positionmgt;

/**
 * @author Jinbo Lin
 * 
 *  
 */
public class PositionmgtDaoHibernate extends HibernateDaoSupport implements
		PositionmgtDao {

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.PositionmgtDao#delete(int)
	 */
	@Override
	public void delete(int id) {
		if (null != this.get(id)) {
			this.getHibernateTemplate().delete(
					this.getHibernateTemplate().get(Positionmgt.class, id));
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.PositionmgtDao#delete(cn.edu.pku.model.Positionmgt)
	 */
	@Override
	public void delete(Positionmgt positionmgt) {
		if (null != this.get(positionmgt.getPositionId())) {
			this.getHibernateTemplate().delete(positionmgt);
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.PositionmgtDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Positionmgt> findAll() {
		String hql = "from Positionmgt";
		return this.getHibernateTemplate().find(hql);
	}
	
	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.PositionmgtDao#get(int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Positionmgt get(int id) {
		String hql = "from Positionmgt positionmgt where positionmgt.positionId=?";
		List<Positionmgt> positionmgts = this.getHibernateTemplate().find(
				hql, new Integer(id));
		Positionmgt positionmgt = (null != positionmgts && 0 != positionmgts.size()) ? positionmgts.get(0) : null;
		return positionmgt;
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.PositionmgtDao#save(cn.edu.pku.model.Positionmgt)
	 */
	@Override
	public Serializable save(Positionmgt positionmgt) {
		if(null == this.get(positionmgt.getPositionId())) {
			this.getHibernateTemplate().save(positionmgt);
		} else {
			positionmgt = null;
		}
		return positionmgt;
		
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.PositionmgtDao#update(cn.edu.pku.model.Positionmgt)
	 */
	@Override
	public void update(Positionmgt positionmgt) {
		if (null != this.get(positionmgt.getPositionId())) {
//			this.getHibernateTemplate().saveOrUpdate(position);
			this.getHibernateTemplate().update(positionmgt);
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.PositionmgtDao#tDelete(int)
	 */
	@Override
	public void tDelete(int id) {
		Positionmgt positionmgt = this.get(id);
		positionmgt.setRemark("Removed");
		this.save(positionmgt);
	}
}
